package club.yuyang.campus2mall.controller;


import club.yuyang.campus2mall.entity.User;
import club.yuyang.campus2mall.service.UserService;
import club.yuyang.campus2mall.utils.Result;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @GetMapping("/list")
    public List<User> userList(){
        return userService.list();
    }

    /**
     * 校验注册的账号是否已存在
     * @param account
     * @return
     */
    @GetMapping("/queryByAccount/{account}")
    public Result queryByAccount(@PathVariable("account") String account){
       User dbUser = userService.getOne(new QueryWrapper<User>().eq("account",account));
       if (dbUser == null){
           return new Result(null,"账号未被注册！","200");
       }else {
           return new Result(null,"该账号已被注册！","401");
       }
    }

    /**用户列表分页查询
     * pagenum:当前页码
     * pagesize:每页显示记录数
     * query: 查找参数
     * @return
     */
    @GetMapping("/yhlb")
    public Result yhlbPage(@RequestParam("pagenum") Integer pagenum,
                            @RequestParam("pagesize") Integer pagesize,
                            @RequestParam("query") String query ){
        IPage<User> userIPage = userService.yhlbPage(pagenum,pagesize,query);
        return new Result(userIPage,"获取分页信息成功！","200");
    }

    /**
     * 根据id查询某个用户
     * @param userId
     * @return
     */
    @GetMapping("/queryById/{userId}")
    public Result queryById(@PathVariable("userId") Long userId){
        User dbUser = userService.getById(userId);
        if (dbUser != null){
            return new Result(dbUser,"获取用户信息成功！","200");
        }
        return new Result(null,"获取用户信息失败！","400");
    }

    /**
     * 根据id删除某个用户（逻辑删除）
     * @param userId
     * @return
     */
    @PostMapping("/delete/{userId}")
    public Result deleteById(@PathVariable("userId") Long userId){
        if (userService.removeById(userId)){
            return new Result(null,"删除成功！","200");
        }
        return new Result(null,"删除失败！","400");
    }

    /**
     * 添加用户
     * @param @RequestBody user
     * @return
     */
    @PostMapping("/add")
    public Result addUser(@RequestBody User user){
        String md5Password = SecureUtil.md5(user.getPassword());
        user.setPassword(md5Password);
        if (userService.save(user)){
            return new Result(null,"添加成功","201");
        }else {
            return new Result(null,"添加失败！","400");
        }
    }

    /**
     * 更新用户
     * @param @RequestBody user
     * @return
     */
    @PostMapping("/update")
    public Result updateUser(@RequestBody User user){
        if (userService.updateById(user)){
            return new Result(null,"用户更新成功","201");
        }else {
            return new Result(null,"用户更新失败！","400");
        }
    }

}
